package com.usian.controller;

import com.usian.feign.ContentFeign;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/content/")
public class ContentController {

    @Autowired
    private ContentFeign contentFeign;

    @RequestMapping("selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Long id) {
        List<TbContentCategory> list = contentFeign.selectContentCategoryByParentId(id);
        if (list != null && list.size() > 0) {
            return Result.ok(list);
        }
        return Result.error("查无数据");
    }

    @RequestMapping("insertContentCategory")
    public Result insertContentCategory(@RequestParam Long parentId, @RequestParam String name) {
        Boolean flag = contentFeign.insertContentCategory(parentId, name);
        if (flag) {
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    @RequestMapping("deleteContentCategoryById")
    public Result deleteContentCategoryById(@RequestParam Long categoryId) {

        try {
            contentFeign.deleteContentCategoryById(categoryId);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }

    }

    @RequestMapping("updateContentCategory")
    public Result updateContentCategory(@RequestParam Long id, @RequestParam String name) {
        Boolean flag = contentFeign.updateContentCategory(id, name);
        if (flag) {
            return Result.ok();
        }
        return Result.error("修改失败");
    }

    @RequestMapping("selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(@RequestParam Long categoryId) {
        PageResult pageResult = contentFeign.selectTbContentAllByCategoryId(categoryId);
        if (pageResult.getResult() != null && pageResult.getResult().size() > 0) {
            return Result.ok(pageResult);
        }
        return Result.error("查无数据");
    }

    @RequestMapping("insertTbContent")
    public Result insertTbContent(TbContent tbContent,@RequestParam String titleDesc) {
        tbContent.setCreated(new Date());
        Boolean flag = contentFeign.insertTbContent(tbContent);
        if (flag) {
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    @RequestMapping("deleteContentByIds")
    public Result deleteContentByIds(@RequestParam Integer[] ids) {

        try {
            contentFeign.deleteContentByIds(ids);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }

    }

}
